home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / AssertLib / AssertLib.h < prev   
Encoding:
C/C++ Source or Header  |  1994-01-11  |  2.7 KB  |  103 lines  |  [TEXT/KAHL]

  1. #pragma once
  2.  
  3. #ifndef NDEBUG
  4.  
  5. /* types of assertions */
  6. enum {
  7.     ASSERT_REQUIRE_KIND,    /* precondition */
  8.     ASSERT_ENSURE_KIND,    /* postcondition */
  9.     ASSERT_CHECK_KIND,    /* checkpoint */
  10.     ASSERT_LAST_KIND
  11. };
  12.  
  13. /* assert option flags control the behavior of assertions */
  14. struct _assert_options {
  15.     Boolean require:1;    /* true enables preconditions */
  16.     Boolean ensure:1;        /* true enables postconditions */
  17.     Boolean check:1;        /* true enables checkpoints */
  18.     Boolean debug:1;         /* true enters debugger on failure */
  19.     Boolean raise:1;        /* true raises exception on failure */
  20.     Boolean print:1;        /* true prints string on failure */
  21.     Boolean stack:1;        /* true prints call stack on failure */
  22.     Boolean trace:1;         /* true traces assertions */
  23. };
  24.  
  25. extern struct _assert_options _assert;
  26.  
  27. int _assert_failed(int type, const char *expr, const char *file, long line);
  28. int _assert_trace(int type, const char *expr, const char *file, long line);
  29.  
  30. /* enable assertions by default */
  31. #ifndef ASSERT_REQUIRE
  32.     #define ASSERT_REQUIRE    (1)
  33. #endif
  34. #ifndef ASSERT_CHECK
  35.     #define ASSERT_CHECK        (1)
  36. #endif
  37. #ifndef ASSERT_ENSURE
  38.     #define ASSERT_ENSURE    (1)
  39. #endif
  40. #ifndef ASSERT_COMPACT
  41.     #define ASSERT_COMPACT    (1)
  42. #endif
  43.  
  44. #ifdef THINK_C
  45.     /* THINK C lets us declare a static variable in a prefix prepended to
  46.         all compiled source files. This saves data space by removing
  47.         multiple instances of the string __FILE__. */
  48.     #define ASSERT_FILE (_assert_file)
  49. #else
  50.     #define ASSERT_FILE __FILE__
  51. #endif
  52.  
  53. #if ASSERT_COMPACT
  54.     /* To save string data space we don't pass the stringified assert
  55.         condition. Since the file and line number are still displayed
  56.         by the assertion handling function we can still locate the
  57.         assertion that failed in the source file. */         
  58.     #define ASSERT_CONDSTR NULL
  59. #else
  60.     #define ASSERT_CONDSTR #cond
  61. #endif
  62.  
  63. /* assertion macros */
  64.  
  65. #if ASSERT_TRACE
  66.     #define _assert_trace_macro(cond, flag, type)    \
  67.         ((flag) && _assert.trace ? \
  68.             _assert_trace(type, ASSERT_CONDSTR, ASSERT_FILE, __LINE__) : 0)
  69. #else
  70.     #define _assert_trace_macro(cond, flag, type)    (0)
  71. #endif
  72.  
  73. #define _assert_check(cond, flag, type) \
  74.     ((void) ((flag) && ! (cond) ? \
  75.         _assert_failed(type, ASSERT_CONDSTR, ASSERT_FILE, __LINE__) : \
  76.         _assert_trace_macro(cond, flag, type)))
  77.  
  78. #if ASSERT_REQUIRE
  79.     #define require(cond)    _assert_check(cond, _assert.require, ASSERT_REQUIRE_KIND)
  80. #else
  81.     #define require(cond)    ((void) 0)
  82. #endif
  83.  
  84. #if ASSERT_ENSURE
  85.     #define ensure(cond)        _assert_check(cond, _assert.ensure, ASSERT_ENSURE_KIND)
  86. #else
  87.     #define ensure(cond)        ((void) 0)
  88. #endif
  89.  
  90. #if ASSERT_CHECK
  91.     #define check(cond)        _assert_check(cond, _assert.check, ASSERT_CHECK_KIND)
  92. #else
  93.     #define check(x)            ((void) 0)
  94. #endif
  95.  
  96. #else /* NDEBUG */
  97.  
  98.     #define require(cond)    ((void) 0)
  99.     #define ensure(cond)        ((void) 0)
  100.     #define check(x)            ((void) 0)
  101.  
  102. #endif /* NDEBUG */
  103.